//
//  MCMarketingListCell.swift
//  StoreManage
//
//  Created by MC on 2018/6/25.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCMarketingListCellClosure = (Int) -> ()


class MCMarketingListCell: UITableViewCell {
    
    var closure : MCMarketingListCellClosure?
    
    
    var model = MCMarketingListModel() {
        didSet {
            titleLabel.text = model.title
            detailLabel.text = model.remark
            activityTimeLabel.text = "活动时间:" + model.startTime.MCString_prefix(index: 10) + " ~ " + model.endTime.MCString_prefix(index: 10) 
            countButton.setTitle(" " + model.marketingPersonCount.stringValue, for: .normal)
            orderButton.setTitle(" " + model.orderCount.stringValue, for: .normal)
            timeLabel.text = model.createTime


            if model.status.intValue == 0 {  // 活动被禁用
                statusLabel.text = "禁用"
                statusLabel.textColor = UIColor.MCGray_middle
            } else {
                if model.activityStatus.intValue == 0 { // 0进行中
                    statusLabel.text = "进行中"
                    statusLabel.textColor = UIColor.MCMain
                } else if model.activityStatus.intValue == 1 {  // 1已完成
                    statusLabel.text = "已结束"
                    statusLabel.textColor = UIColor.MCGray_middle
                } else {  // 2未开始
                    statusLabel.text = "未开始"
                    statusLabel.textColor = UIColor.MCGray_middle
                }
            }
            
            
            self.colleciontView.reloadData()

        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.addSubview(statusLabel)
        statusLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.height.equalTo(20)
            make.top.equalTo(10)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.lessThanOrEqualTo(statusLabel.snp.left).offset(-5)
            make.centerY.equalTo(statusLabel)
        }
        
        self.addSubview(detailLabel)
        detailLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(20)
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
        }
        
        
        self.addSubview(activityTimeLabel)
        activityTimeLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(17)
            make.top.equalTo(detailLabel.snp.bottom).offset(1)
        }

        self.addSubview(colleciontView)
        colleciontView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.top.equalTo(activityTimeLabel.snp.bottom).offset(10)
            make.height.equalTo(145)
        }
        
        self.addSubview(lineOneView)
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.top.equalTo(colleciontView.snp.bottom).offset(10)
            make.height.equalTo(1)
        }
        
        self.addSubview(lineTwoView)
        lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(5)
        }
        
        self.addSubview(countButton)
        countButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(lineOneView.snp.bottom)
            make.bottom.equalTo(lineTwoView.snp.top)
        }
        
        self.addSubview(orderButton)
        orderButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(countButton.snp.right).offset(20)
            make.centerY.equalTo(countButton)
        }
        

        
        self.addSubview(timeLabel)
        timeLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.centerY.equalTo(countButton)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func itemClicked() {
        print("点击了")
        
        if closure != nil {
            self.closure?(model.activityId.intValue)
        } else {
            print("closure 为nil")
        }
    }
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(16)
        label.textColor = UIColor.MCWords
        return label
    }()
    
    lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.MCMain
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(13)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
    
    lazy var activityTimeLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
    
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
    
    lazy var countButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Marketing_person"), for: .normal)
        button.titleLabel?.font = MCFont(12)
        button.setTitleColor(UIColor.MCGray_middle, for: .normal)
        return button
    }()

    lazy var orderButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Marketing_order"), for: .normal)
        button.titleLabel?.font = MCFont(12)
        button.setTitleColor(UIColor.MCGray_middle, for: .normal)
        return button
    }()
    
    lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    
    private lazy var colleciontView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = UICollectionViewScrollDirection.horizontal
        layout.sectionInset = UIEdgeInsetsMake(7, 7, 7, 7)
        layout.itemSize = CGSize.init(width: 103, height: 145)
        layout.minimumLineSpacing = 7
        layout.minimumInteritemSpacing = 0
        
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.register(MCResueGoodsCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(itemClicked))
        collectionView.isUserInteractionEnabled = true
        collectionView.addGestureRecognizer(tap)
        
        return collectionView
    }()

    fileprivate lazy var dataArray: NSMutableArray = {
        let arrayM = NSMutableArray.init()
        return arrayM
    }()

}


extension MCMarketingListCell: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return model.goodsList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)  as! MCResueGoodsCell
        
        for view in cell.contentView.subviews {
            view.removeFromSuperview()
        }
        
        
    
        
        let dict = self.model.goodsList[indexPath.row] as? [String:Any] ?? [:]
        
        let model = MCResueGoodsModel()
        model.goodsImage = dict["navigateImg1"] as? String ?? ""
        model.goodsName = dict["goodsName"] as? String ?? ""
        model.salePrice = dict["salePrice"] as? NSNumber ?? 0
        model.suggestedPrice = dict["suggestedPrice"] as? NSNumber ?? 0

        
        cell.model = model
        
        return cell
    }
    
//    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//
//        print("点击了")
//
//    }
}

